/* @(#)ImageController.java v${project.version} 2011-6-28 下午1:23:40 
 * 
 * ${project.copyright.info}
 * ${project.copyright}
 * ==============================================================================================================*/
package net.lysa.ooo.controller;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Hashtable;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.sanselan.ImageFormat;
import org.apache.sanselan.ImageWriteException;
import org.apache.sanselan.Sanselan;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>Class description goes here.</p>
 * 
 * @see    java.lang.Object
 * @author caochuan@guahaoe.com
 */
@Controller
@RequestMapping("/images/*")
public class ImageController {
	Logger logger = LoggerFactory.getLogger(this.getClass());
	//OK @RequestMapping(value="face/{id}",method=RequestMethod.GET)
	@RequestMapping(value="face/{id}",method=RequestMethod.GET,produces="image/png")
	public void face(@PathVariable String id,OutputStream os) throws IOException{
		logger.info(id);
		URL url = this.getClass().getClassLoader().getResource("zhuque_face.jpg");
		BufferedImage bi = ImageIO.read(url);
		Graphics2D g = (Graphics2D)bi.getGraphics();
		g.setColor(Color.BLACK);
		g.drawString("TEST 中文", bi.getWidth()-60, bi.getHeight()-12);
		ImageIO.write(bi, "png", os);
		//BufferedImage bi = Sanselan.getBufferedImage(url.openStream());
		//BufferedImage bi = ImageIO.read(url);
		///Map params = new Hashtable();
		//Sanselan.writeImageToBytes(bi, ImageFormat.IMAGE_FORMAT_PNG, params);
		
	}
	
	@RequestMapping(value="chart",method=RequestMethod.GET)
	public void chart(OutputStream os) throws IOException{
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("Category 1", 43.2);
		dataset.setValue("Category 2", 27.9);
		dataset.setValue("Category 3", 79.5);
		// create a chart...
		JFreeChart chart = ChartFactory.createPieChart(
		"Sample Pie Chart",
		dataset,
		true, // legend?
		true, // tooltips?
		false // URLs?
		);
		
		BufferedImage bi = chart.createBufferedImage(200, 200);
		ImageIO.write(bi, "png", os);
		
	}
	
	@RequestMapping("hi")
	public @ResponseBody String hi(){
		return "Hi .. .. . ";
	}
	
	@RequestMapping("test")
	public void test(OutputStream os) throws IOException, ImageWriteException {
		URL url = this.getClass().getClassLoader().getResource("zhuque_face.jpg");
		//System.out.println(url);
		BufferedImage bi = ImageIO.read(url);
		Map params = new Hashtable();
		Sanselan.writeImageToBytes(bi, ImageFormat.IMAGE_FORMAT_PNG, params);
		//os.write("Wrote binary response using OutputStream".getBytes());
	}
}
